﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using ePlanet.ArchiveMange.Services;
using ePlanet.ArchiveMange.Models;
using System.Data;

namespace Web.Views.ArchiveBorrow
{
    public partial class ArchiveBorrowMenuManageEdit : PageBase
    {
        [Dependency]
        public IArchiveMenuService MenuService { get; set; }

        /// <summary>
        /// 请求变量
        /// </summary>
        public string m_User_ID
        {
            get
            {
                if (Request.QueryString["User_ID"] != null)
                {
                    return Request.QueryString["User_ID"];
                }
                else
                {
                    return null;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCeckBoxList();
                CheckArchiveMenus();
            }
        }

        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {

            List<string> list = new List<string>();
            foreach (ListItem item in cblArchiveMenu.Items)
            {
                if (item.Selected == true)
                {
                    list.Add(item.Value);
                    if (string.Equals(item.Value.Trim(), "1"))
                    {
                        list.Add("101");
                        list.Add("102");
                    }
                    if (string.Equals(item.Value.Trim(), "6"))
                    {
                        list.Add("61");
                        list.Add("62");
                        list.Add("63");
                        list.Add("64");
                    }
                    if (string.Equals(item.Value.Trim(), "7"))
                    {
                        list.Add("71");
                        list.Add("72");
                        list.Add("7101");
                        list.Add("7102");
                    }
                    if (string.Equals(item.Value.Trim(), "8"))
                    {
                        list.Add("81");
                        list.Add("82");
                        list.Add("83");
                    }
                    if (string.Equals(item.Value.Trim(), "9"))
                    {
                        list.Add("91");
                        list.Add("92");
                        list.Add("93");
                        list.Add("94");
                    }
                }
            }

            this.Add(list);

            Response.Redirect("ArchiveBorrowMenuManage.aspx?isBase=" + Request.QueryString["isBase"]);
        }

        /// <summary>
        /// 返回按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ArchiveBorrowMenuManage.aspx?isBase=" + Request.QueryString["isBase"]);
        }

        /// <summary>
        /// 绑定绑定复选框列
        /// </summary>
        private void BindCeckBoxList()
        {
            cblArchiveMenu.DataSource = LoadData();
            cblArchiveMenu.DataTextField = "ARCHIVEMENU";
            cblArchiveMenu.DataValueField = "TYPEID";
            cblArchiveMenu.DataBind();
        }

        /// <summary>
        /// 更新选中状态
        /// </summary>
        private void CheckArchiveMenus()
        {
            DataSet ds = LoadRoleArchive(m_User_ID);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    foreach (ListItem item in cblArchiveMenu.Items)
                    {
                        if (item.Value == row["archivemenuid"].ToString())
                        {
                            item.Selected = true;
                            break;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 加载所有的基本档案编目
        /// </summary>
        private DataTable LoadData()
        {
            ArchiveMangeModal.ARCHIVEMENUDataTable dt = MenuService.GetAllBaseMenu();
            return dt;
        }

        private DataSet LoadRoleArchive(string user_ID)
        {
            try
            {
                string sql = "select * from ARCHIVEBORROWMENU t where t.User_id='" + user_ID + "'";
                IMDataHelper dal = new IMDataHelper();
                DataSet ds = dal.DBHelper.ExecuteDataSet(sql);
                return ds;
            }
            catch
            {
                return null;
            }
        }

        private void Add(List<string> menuIDList)
        {
            IMDataHelper dal = new IMDataHelper();
            string deleteAll = string.Format("delete from ARCHIVEBORROWMENU where User_ID='{0}'", this.m_User_ID);
            dal.DBHelper.ExecuteSql(deleteAll);
            string addSql = "insert into ARCHIVEBORROWMENU (USER_ID, ARCHIVEMENUID, EDITUSERID) values('{0}',{1},'{2}')";
            foreach (string str in menuIDList)
            {
                dal.DBHelper.ExecuteSql(string.Format(addSql, this.m_User_ID, str, DateTime.Now));
            }
        }
    }
}